$(function() {
	$('#searchInput').AutoComplete(
					{
						'data' : ctx+"/knowledgebase/knowlegeAction!handleKeyword.action",
						'ajaxDataType' : 'json',
						'maxItems' : 10, //最多显示多少项
						'delay' : 2000,
						'async' : true,
						'width' : 'auto',
						'emphasisHandler' : function(keyword, data) {
							var regex = RegExp("("
									+ keyword.replace(/([.?*+^$[\]\\(){}|-])/g,
											"\\$1") + ")", 'ig');
							data.label = data.LABEL
									.replace(regex,
											"<span style='font-weight:bold;color:red;font-size:15px'>$1</span>");
							data.value = data.VALUE;
						},
						'onerror' : function(msg) {
							alert(msg);
						}
					});
 
	$.ajax({
		url:ctx+"/knowledgebase/knowlegeAction!initKnowledgeSrc.action",
		type:"post",
		data:{codeType:"requireSrc",tableName:"TD_SPEED_REPORT_DEFINE"},
		dataType:"json",
		error: function(){alert('请求超时，请稍候再试');},
		success: function(result){
			var s = new StringBuffer();
			s.append("<a href='#' myval=''>全部来源</a>");
			$.each(result,function(index,value){
				s.append("<a href='#' myval='").append(value.codeValue).append("'>").append(value.codeName).append("</a>");
			});
			$("#assortId").html(s.toString());
			
			//知识类型切换
			$(".icon_sj").click(function() {
				$("#assortId").toggle();
			}); 
			$("#assortId a").click(function() {
				$("#assortContent").text($(this).html());
				$("#knowledgeSource").val($(this).attr("myval")); 
				$("#assortId").css("display", "none");
			});  
			var t;
			$("#assortId").mouseout(function(){
				clearTimeout(t);
		    	t = setTimeout(function(){
					$("#assortId").css("display", "none");
				},500); 
			}).mouseover(function(){
				clearTimeout(t);
			});
			
			$(".icon_sj").mouseout(function(){
				t = setTimeout(function(){
					$("#assortId").css("display", "none");
				},500); 
			});
		}
	});
	
	//搜索操作
	$("#searchBtn").click(function(){
		var searchContent = $.trim($("#searchInput").val()); 
		showGridList({rows:10,keyWord:searchContent,knowledgeSource:$("#knowledgeSource").val()});
	});
	showGridList({rows:10});
});
/**************************************************************/
function showGridList(data){
	$("#pageNavigation").pager({
		display : 8,			  //最多显示多少页	
		data:data,           //rows 每页显示多少条数
		url: ctx+"/knowledgebase/knowlegeAction!showKnowledgeForPage.action",
		showDetail:true,                  //是否显示详细条数 默认为false 
		onResult : function(data){  
			var s = new StringBuffer();
			if(data!=null){
				$.each(data,function(index,value){
					var knowledgeDesc = value.knowledgeDesc;  //知识描述
					var evaluate = "0";            //知识评分
					if(value.evaluate){
						evaluate = value.evaluate;
					}
					if(knowledgeDesc&&knowledgeDesc.length>145){
						knowledgeDesc = knowledgeDesc.slice(0, 145)+"...";
					}
					var pinfen = "";
					if(evaluate!='undefined'&& $.trim(evaluate)!=''){
						pinfen = handlePinfen(evaluate);
					}else{
						pinfen =handlePinfen("0");
					}  
					var knowledgeTitle = $.trim(value.knowledgeName);   
					s.append("<table class='kTable'><tr>"); 
					s.append("<td colspan='6' style='height:25px;padding:0;margin:0;text-align: left;'><div class='titleDiv'><span class='titleSpan'  onclick=\"doDetail(\'").append(value.knowledgeId).append("\')\">").append(knowledgeTitle).append("</span></div></td>");
					s.append("<td width='35' style='background-color:#F7F6F6;width:35px;border-left:dashed #a7b0b2 1px;' rowspan='3' ><div class='scoreDiv'>").append(pinfen).append("</div></td></tr>");
					s.append("<tr><td width='74' height='35px'><div class='kDesc' title='").append(value.knowledgeDesc).append("('>知识描述:</div></td>");
					s.append("<td height='35px' colspan='5'><div  class='descDiv'>").append(knowledgeDesc).append("</div></td></tr>");
					s.append("<tr><td class='ktdR'>知识类别:</td>"); 
					s.append("<td class='ktdL'>").append(value.knowledgeOneTypeName).append('-').append(value.knowledgeTwoTypeName).append("</td>");
					s.append("<td class='ktdR'>创建时间:</td>");
					s.append("<td class='ktdL'>").append(value.createTime).append("</td>");
					s.append("<td class='ktdCreatorR'>知识作者：</td>");
					s.append("<td class='ktdCreatorL'>").append(value.creator).append("</td>");
					s.append("</tr></table>"); 
			  });
			}else{ 
				//s.append("<tr><td colspan='6'><div style='padding-top:70px;height:350px;text-align:center;'><img src='").append(ctx).append("/images/icons/wushuju.png'/></div></td></tr>");
				s.append("<div style='padding-top:100px;height:250px;overflow:hidden;'><img src='").append(ctx).append("/dqm/knowledgebase/images/wushuju.png'/></div>");
			}
			$("#mytbody").html(s.toString());
       }   
	});  
}
/**************************************************************/


//查看详细
function doDetail(id){
	window.top.$('<div/>').dialog({
		href:ctx+'/knowledgebase/knowlegeAction!toQueryKnowledgeDetail.action?knowledgeId='+id,
		top:($(window).height()-450)*0.5,
		left:($(window).width()-600)*0.5,
		width:800,
		modal:true,
		title:'知识详情',  	
//		buttons:[{
//			text : '关闭',
//			iconCls : '',
//			handler:function(){
//				var d=window.top.$(this).closest('.window-body');
//				d.dialog('destroy'); 
//			}
//		}], 
		onClose:function(){
			window.top.$(this).dialog('destroy');
		}
	});
}

//拼接评分五角星
function handlePinfen(evaluate){
	var imagStar = ctx+'/dqm/knowledgebase/images/star.png';
	var imagStar_empty = ctx+'/dqm/knowledgebase/images/star_empty.png';
	var s = new StringBuffer();
	switch(evaluate){
		case "0":
			s.append("<img src='").append(imagStar_empty).append("' title='尚未评价'/>");
			s.append("<img src='").append(imagStar_empty).append("' title='尚未评价'/>");
			s.append("<img src='").append(imagStar_empty).append("' title='尚未评价'/>");
			s.append("<img src='").append(imagStar_empty).append("' title='尚未评价'/>");
			s.append("<img src='").append(imagStar_empty).append("' title='尚未评价'/>");
			break;
		case "1":
			s.append("<img src='").append(imagStar).append("' title='很差'/>");
			s.append("<img src='").append(imagStar_empty).append("' title='很差'/>");
			s.append("<img src='").append(imagStar_empty).append("' title='很差'/>");
			s.append("<img src='").append(imagStar_empty).append("' title='很差'/>");
			s.append("<img src='").append(imagStar_empty).append("' title='很差'/>");
			break;
		case "2":
			s.append("<img src='").append(imagStar).append("' title='一般'/>");
			s.append("<img src='").append(imagStar).append("' title='一般'/>");
			s.append("<img src='").append(imagStar_empty).append("' title='一般'/>");
			s.append("<img src='").append(imagStar_empty).append("' title='一般'/>");
			s.append("<img src='").append(imagStar_empty).append("' title='一般'/>");
			break;
		case "3":
			s.append("<img src='").append(imagStar).append("' title='还好'/>");
			s.append("<img src='").append(imagStar).append("' title='还好'/>");
			s.append("<img src='").append(imagStar).append("' title='还好'/>");
			s.append("<img src='").append(imagStar_empty).append("' title='还好'/>");
			s.append("<img src='").append(imagStar_empty).append("' title='还好'/>");
			break;
		case "4":
			s.append("<img src='").append(imagStar).append("' title='较好'/>");
			s.append("<img src='").append(imagStar).append("' title='较好'/>");
			s.append("<img src='").append(imagStar).append("' title='较好'/>");
			s.append("<img src='").append(imagStar).append("' title='较好'/>");
			s.append("<img src='").append(imagStar_empty).append("' title='较好'/>");
			break;
		case "5":
			s.append("<img src='").append(imagStar).append("' title='很好'/>");
			s.append("<img src='").append(imagStar).append("' title='很好'/>");
			s.append("<img src='").append(imagStar).append("' title='很好'/>");
			s.append("<img src='").append(imagStar).append("' title='很好'/>");
			s.append("<img src='").append(imagStar).append("' title='很好'/>");
			break;
		 default: return;
	}
	return s.toString();
}
//字符拼接
function StringBuffer(){
	this.__string__ = new Array();
}
StringBuffer.prototype.append = function(str){
	this.__string__.push(str);
	return this;
}
StringBuffer.prototype.toString = function(){
	return this.__string__.join("");
}